package it.unical.mat.aec.gui;

import it.unical.mat.aec.manageResult.EsportaTestsTxT;
import it.unical.mat.aec.manageResult.ManageResultFacade;
import it.unical.mat.aec.manageResult.OrdinaException;
import it.unical.mat.aec.manageResult.OrdinaTemplateMethod;
import it.unical.mat.aec.model.Test;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.print.PrinterException;
import java.io.FileNotFoundException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

public class ModificaPanel extends JPanel {

	private JTable studentTable;
	private JComboBox comboRicercaBox;
	private Object ricerche[]={"Ricerca per Studente","Ricerca per Matricola"};

	public ModificaPanel() {
		super(new BorderLayout());
		JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		studentTable = new JTable(new TestValutationTableModel(
				ManageResultFacade.getInstance().getTests()));
		studentTable.setDefaultRenderer(Object.class, new JTableRender());
		pane.add(new JScrollPane(studentTable), JSplitPane.RIGHT);
		JPanel upPanel = new JPanel();
		upPanel.setLayout(new BorderLayout());
		JPanel panel = new JPanel();
		upPanel.add(panel, BorderLayout.WEST);
		pane.add(upPanel, JSplitPane.LEFT);
		addComponent(panel);
		add(pane, BorderLayout.CENTER);
		studentTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount()>=2)
					MainFrame.getInstance().VisualizzaTestPanel(
							studentTable.getSelectedRow());
			}
		});
		addSearch();
	}

	private void addSearch() {
		JPanel searchPanel = new JPanel(new BorderLayout());
		JPanel searchFlowPanel = new JPanel();
		final JTextField fieldSearch = new JTextField(15);
		JButton searchButton = new JButton("Cerca");
		searchButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					if(comboRicercaBox.getSelectedIndex()==1){
						Test test = ManageResultFacade.getInstance()
								.getTestMatricola(
										Integer.valueOf(fieldSearch.getText()));
						if (test == null)
							JOptionPane.showMessageDialog(ModificaPanel.this,
									"Nessun test trovato", "Informazione",
									JOptionPane.INFORMATION_MESSAGE);
						else
							studentTable
									.setModel(new TestValutationTableModel(test));
					}else if(comboRicercaBox.getSelectedIndex()==0){
						ArrayList<Test> tests=ManageResultFacade.getInstance().getTestStudente(fieldSearch.getText());
						if(tests.size()==0)
							JOptionPane.showMessageDialog(ModificaPanel.this,
									"Nessun test trovato", "Informazione",
									JOptionPane.INFORMATION_MESSAGE);
						else
							studentTable
									.setModel(new TestValutationTableModel(tests));
					}
					repaint();
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(ModificaPanel.this,
							"Nessun test trovato", "Informazione",
							JOptionPane.INFORMATION_MESSAGE);
				}
			}
		});
		JButton removeSearchButton = new JButton("Rimuovi ricerca");
		removeSearchButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
					studentTable.setModel(new TestValutationTableModel(ManageResultFacade.getInstance().getTests()));
				repaint();
			}
		});
		comboRicercaBox=new JComboBox(ricerche);
		searchFlowPanel.add(comboRicercaBox);
		searchFlowPanel.add(fieldSearch);
		searchFlowPanel.add(searchButton);
		searchFlowPanel.add(removeSearchButton);
		searchPanel.add(searchFlowPanel, BorderLayout.WEST);
		add(searchPanel, BorderLayout.SOUTH);
	}

	private void addComponent(JPanel panel) {

		Dimension dimension=new Dimension(80, 80);


		panel.add(IconButton.getLabelButton(
				"images/icon/visualizzaTraccia.png", dimension,
				"Visualizza Test", new LabelButtonEvent() {

					@Override
					public void event() {
						if (studentTable.getSelectedRow() == -1) {
							JOptionPane.showMessageDialog(ModificaPanel.this,
									"Nessun test selezionato", "Error",
									JOptionPane.ERROR_MESSAGE);
						} else
							MainFrame.getInstance().VisualizzaTestPanel(
									studentTable.getSelectedRow());
					}
				}));
		panel.add(IconButton.getLabelButton("images/icon/ordina.png",
				dimension, "Ordina tests", new LabelButtonEvent() {

					@Override
					public void event() {
						InputOrdinaFrame frame = new InputOrdinaFrame(
								ModificaPanel.this);
					}
				}));
		panel.add(IconButton.getLabelButton("images/icon/cancellaTests.png",
				dimension, "Cancella tutti i tests", new LabelButtonEvent() {

					@Override
					public void event() {
						ManageResultFacade facade = ManageResultFacade
								.getInstance();
						int response = JOptionPane
								.showConfirmDialog(ModificaPanel.this,
										"Le traccia e il file verranno eliminati\n\nVuoi continuare ?");
						if (response == JOptionPane.OK_OPTION) {
							facade.getTests().clear();
							String pathFile = facade.getPathFile();
							if (pathFile != null) {
								facade.cancellaTests(pathFile);
								JOptionPane.showMessageDialog(
										ModificaPanel.this,
										"Cancellazione avvenuta con successo",
										"Informazione",
										JOptionPane.INFORMATION_MESSAGE);
							} else
								JOptionPane
										.showMessageDialog(
												ModificaPanel.this,
												"Impossibile eliminare il file , file non trovato",
												"Error",
												JOptionPane.ERROR_MESSAGE);

							MainFrame.getInstance().startPanel();
						}
					}
				}));
		panel.add(IconButton.getLabelButton("images/icon/salva.png", dimension,
				"Salva", new LabelButtonEvent() {

					@Override
					public void event() {
						String path = CaricaFile
								.getDirectory(ModificaPanel.this);
						if (path != null) {
							if (ManageResultFacade.getInstance().salvaTests(
									path + ".aec"))
								JOptionPane.showMessageDialog(
										ModificaPanel.this,
										"Salvataggio completato",
										"Informazione",
										JOptionPane.INFORMATION_MESSAGE);
						}

					}
				}));
		panel.add(IconButton.getLabelButton("images/icon/stampa.png",
				dimension, "Stampa tests", new LabelButtonEvent() {

					@Override
					public void event() {
						try {
							if (studentTable.print())
								JOptionPane.showMessageDialog(
										ModificaPanel.this, "Avvio Stampa",
										"Informazione",
										JOptionPane.INFORMATION_MESSAGE);
							else
								JOptionPane.showMessageDialog(
										ModificaPanel.this,
										"Stampa non effettuata",
										"Informazione",
										JOptionPane.INFORMATION_MESSAGE);
						} catch (PrinterException e) {
							JOptionPane.showMessageDialog(ModificaPanel.this,
									e.getLocalizedMessage(), "Error",
									JOptionPane.ERROR_MESSAGE);
						}
					}
				}));
		panel.add(IconButton.getLabelButton("images/icon/esporta.png",
				dimension, "Esporta TxT", new LabelButtonEvent() {

					@Override
					public void event() {
						String path = CaricaFile
								.getDirectory(ModificaPanel.this);
						if (path != null)
							try {
								ManageResultFacade.getInstance()
										.setEsportaTest(new EsportaTestsTxT());
								ManageResultFacade.getInstance().esportaTests(
										path + ".txt");
							} catch (FileNotFoundException e) {
								JOptionPane.showMessageDialog(
										ModificaPanel.this,
										e.getLocalizedMessage(), "Error",
										JOptionPane.ERROR_MESSAGE);
							}

					}
				}));
	}

	public void ordina(String tipo, boolean crescente) {
		try {
			studentTable.setModel(new TestValutationTableModel(
					ManageResultFacade.getInstance().ordinaTests(tipo,
							crescente)));
			repaint();
		} catch (OrdinaException e) {
			JOptionPane
					.showMessageDialog(ModificaPanel.this,
							e.getLocalizedMessage(), "Error",
							JOptionPane.ERROR_MESSAGE);
		}
	}
}
